# -*- coding = utf-8 -*-
# @Time : 2023/3/13 3:53
# @Author : 龙王赘婿_彪少
# @File : modbus_demo1.py
# @Software: PyCharm

# 功能：

import serial
import modbus_tk.defines as cst
from modbus_tk import modbus_rtu
from modbus_tk import modbus_tcp

PORT = "com1"


class Modbus:
    __doc__ = ["https://blog.csdn.net/pista/article/details/121911024"]
    __doc__ += ["看了一圈发现modbus_tk应该是最强大的，故用此模块"]
    __doc__ += ["01 READ_COILS 读线圈"]
    __doc__ += ["02 READ_DISCRETE_INPUTS 读离散输入"]
    __doc__ += ["03 READ_HOLDING_REGISTERS 读保持寄存器"]
    __doc__ += ["04 READ_INPUT_REGISTERS 读输入寄存器"]
    __doc__ += ["05 WRITE_SINGLE_COIL 写单一线圈"]
    __doc__ += ["06 WRITE_SINGLE_REGISTER 写单一寄存器"]
    __doc__ += ["15 WRITE_MULTIPLE_COILS 写多个线圈"]
    __doc__ += ["16 WRITE_MULTIPLE_REGISTERS 写多寄存器"]

    # execute(slave, function_code, starting_address, quantity_of_x=0,
    # output_value=0, data_format="",expected_length=-1, write_starting_address_FC23=0)

    def __init__(self):
        self.master = modbus_rtu.RtuMaster(
            serial.Serial(port=PORT, baudrate=9600, bytesize=8, parity='N', stopbits=1, xonxoff=0))

        # self.master = modbus_tcp.TcpMaster(host="127.0.0.1", port=9000)
        self.master.set_timeout(1.0)

    def read_coils(self):
        # 读线圈
        try:
            res = self.master.execute(1, cst.READ_COILS, starting_address=0, quantity_of_x=20)
            print(res)
        except Exception as err:
            print("异常：", err)

    def read_discrete_coils(self):
        # 读只读线圈
        try:
            res = self.master.execute(1, cst.READ_DISCRETE_INPUTS, starting_address=0, quantity_of_x=20)
            print(res)
        except Exception as err:
            print("异常：", err)

    # 写单线圈
    def write_sigle_coil(self):
        try:
            res = self.master.execute(1, cst.WRITE_SINGLE_COIL, starting_address=7, output_value=1)
            print(res)  # 65280 FF00
        except Exception as err:
            print("异常：", err)

    # 写多线圈
    def write_multiple_coils(self):
        try:
            res = self.master.execute(1, cst.WRITE_MULTIPLE_COILS, starting_address=0, output_value=(1, 0, 1, 0, 1))
            print(res)  # 65280 FF00
        except Exception as err:
            print("异常：", err)

    # 读保持寄存器 用的最多 0x03
    def read_holding_resasters(self):
        try:
            res = self.master.execute(1, cst.READ_HOLDING_REGISTERS, starting_address=0, quantity_of_x=10)
            print(res)
        except Exception as err:
            print("异常：", err)

    # 读输入寄存器 （只读）
    def read_input_resasters(self):
        try:
            res = self.master.execute(1, cst.READ_INPUT_REGISTERS, starting_address=0, quantity_of_x=10)
            print(res)
        except Exception as err:
            print("异常：", err)

    # 写单寄存器 06
    def write_single_register(self):

        try:
            res = self.master.execute(1, cst.WRITE_SINGLE_REGISTER, starting_address=10, output_value=111)
            print(res)  # 65280 FF00
        except Exception as err:
            print("异常：", err)

    # 写多寄存器 元组写入，无需指定大小
    def write_mutiple_registers(self):
        try:
            res = self.master.execute(1, cst.WRITE_MULTIPLE_REGISTERS, starting_address=11,
                                      output_value=(14, 12, 13, 14))
            print(res)
        except Exception as err:
            print("异常：", err)


if __name__ == "__main__":
    m = Modbus()
    m.write_single_register()
